package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Quickresponse;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.QuickResponseService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Answer {
    CourseService courseService=CilentProxy.getProxyInstance(CourseService.class);
    QuickResponseService quickResponseService= CilentProxy.getProxyInstance(QuickResponseService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("抢答");
            System.out.println("1. 抢答列表");
            System.out.println("2. 抢答");
            System.out.println("3. 我的抢答");
            System.out.println("0. 返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allAnswer(course);
                    break;
                case 2:
                    selectAnswer(course);
                    break;
                case 3:
                    myAnswer(course);
                    break;
                case 0:
                    return;
                default:
                    break;
            }
        }
    }

    private void myAnswer(Course course) {
        while (true){
            Result result = quickResponseService.myQuickResponses(AppData.student.getUserId(), course.getId());
            if (result.getCode()==200){
                List<Quickresponse> obj = (List<Quickresponse>) result.getObj();
                for (Quickresponse quickresponse : obj) {
                    Result byId = courseService.findById(quickresponse.getCourseId());
                    if (byId.getCode()==200){
                        Course obj1 = (Course) byId.getObj();
                        if (obj1!=null){
                            System.out.println("id:"+quickresponse.getId()+"   问题"+quickresponse.getQuestion()+"     课程名称"+obj1.getName());
                        }
                    }

                }
            }else {
                System.out.println("加载我的抢答列表失败");
            }
            break;
        }
    }


    private void allAnswer(Course course) {
        while (true){
            Result result = quickResponseService.allQuick(course.getId());
            if (result.getCode()==200){
                System.out.println("加载抢答列表成功");
                List<Quickresponse> obj = (List<Quickresponse>) result.getObj();
                for (Quickresponse quickresponse : obj) {
                    System.out.println((quickresponse.getStudentId()==null?"未抢答":"已抢答")+"     "+quickresponse);
                }
            }else {
                System.out.println("加载抢答列表失败");
            }
            break;
        }
    }
    private void selectAnswer(Course course) {
        while (true){
            List<Quickresponse> quickresponseList=new ArrayList<>();
            Result result = quickResponseService.allQuick(course.getId());
            if (result.getCode()==200){
                System.out.println("加载抢答列表成功");
                List<Quickresponse> obj = (List<Quickresponse>) result.getObj();
                for (Quickresponse quickresponse : obj) {

                        quickresponseList.add(quickresponse);

                }
            }else {
                System.out.println("加载抢答列表失败");
            }
            for (Quickresponse quickresponse : quickresponseList) {
                System.out.println((quickresponse.getStudentId()==null?"未抢答":"已抢答")+"     抢答id"+quickresponse.getId());
            }
            System.out.println("请选择想要抢答的id");
            int id = scanner.nextInt();
            boolean a=false;
            for (Quickresponse quickresponse : quickresponseList) {
                if (id==quickresponse.getId()){
                    a=true;
                }
            }
            if (a){
                Result result1 = quickResponseService.quickResponses(id, AppData.student.getId());
                if (result1.getCode()==200){
                    System.out.println("抢答成功");
                }else {
                    System.out.println(result1.getMsg());
                }
            }
            break;
        }

    }
}
